/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package andreafrancia.remoted;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;

/**
 *
 * @author Andrea Francia andrea.francia at gmx dot org
 */
public class Remotizer {

    /**
     * Create a {@link RemoteList} suitable to be returned by an RMI method.
     * 
     * @param <T> the type of the element of the list
     * @param list the actual list tha will be remotized.
     * @return a RemotedList that act as a proxy for the specified list
     * @throws RemotizationFailedException if a {@link RemoteException} occours
     *         during the object export, 
     *         {@see java.rmi.server.UnicastRemoteObject#exportObject()}.
     */
    public static <T> RemoteListAsList<T> remotize(List<T> list)
            throws RemotizationFailedException {
        ListAsRemoteList<T> proxyList = new ListAsRemoteList<T>(list);
        try {
            @SuppressWarnings("unchecked")
            RemoteList<T> exported = (RemoteList<T>) UnicastRemoteObject.exportObject(proxyList, 0);
            return new RemoteListAsList<T>(exported);
        } catch (RemoteException ex) {
            throw new RemotizationFailedException().initCause(ex);
        }
    }
}
